import httpFetch from "share/httpFetch";
import config from "config";
import { formatNumber, messages } from "share/common";
import errorMessage from "share/errorMessage";
import { commonBarReportFactory, loanTendencyReportFactory } from "../echarts-data-transfer";

export default {
  // 获取预览图表
  getTotalAnalysisReport(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/loan/analysis/report/total', params)
        .then((response) => {
          let dataLength = 1;
          const { loanTotalAmount, loanTotalCount, loanBalance } = response.data;
          let options = [{
            title: messages('finance-7.key203')/*累计借款金额*/,
            detail: loanTotalAmount ? formatNumber(loanTotalAmount) : 0
          }, {
            title: messages('finance-7.key204')/*实时借款金额*/,
            detail: loanBalance ? formatNumber(loanBalance) : 0
          }, {
            title: messages('finance-7.key205')/*总借款人数（含供应商）*/,
            detail: loanTotalCount
          }];
          resolve({
            dataLength,
            options
          });
        })
        .catch((err) => {
          errorMessage(err.response || err);
          reject(err.response);
        })
    })
  },
  // 趋势表
  getLoanTendencyReport(params) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/loan/analysis/report/total/tendency', params)
        .then((response) => {
          let { data } = response;
          data = data || [];
          let options = loanTendencyReportFactory(data);
          let dataLength = data.length;
          resolve({
            options,
            dataLength
          });
        })
        .catch((err) => {
          errorMessage(err.response || err);
          reject(err.response);
        })
    })
  },
  // 借款类型表
  getLoanTypeReport(params) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/loan/analysis/report/loan/type/analysis', params)
        .then((response) => {
          let { data } = response;
          data = data || [];
          //兼容性处理
          let options = commonBarReportFactory(data, 0, undefined, true);
          options.series[0].name = messages('finance-7.key310')/*金额(元)*/;
          options.legend.data = [messages('finance-7.key310')/*金额(元)*/];
          let dataLength = data.length;
          resolve({
            options,
            dataLength
          });
        })
        .catch((err) => {
          errorMessage(err.response || err);
          reject(err.response);
        })
    })
  },
  // 借款分析表
  getLoanAnalysisReport(params) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/loan/analysis/report/loan/analysis', params)
        .then((response) => {
          let { data } = response;
          data = data || [];
          data.forEach(item => {
            let { filterPayeeType, filterPayeeId } = item;
            item.criteria = {
              filterPayeeType,
              filterPayeeId
            }
          });
          let options = commonBarReportFactory(data, 1, undefined, true);
          options.series[0].name = messages('finance-7.key310')/*金额(元)*/;
          options.legend.data = [messages('finance-7.key310')/*金额(元)*/];
          let dataLength = data.length;
          resolve({
            options,
            dataLength
          });
        })
        .catch((err) => {
          errorMessage(err.response || err);
          reject(err.response);
        })
    })
  },

  // 借款明细表
  getLoanDetailReport(params, pagination) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/loan/analysis/report/loan/detail/list' +
        '?page=' + pagination.page + "&size=" + pagination.size, params)
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  // 借款余额表
  getLoanBalanceDetail(params, pagination) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/loan/analysis/report/loan/balance/detail/list' +
        '?page=' + pagination.page + "&size=" + pagination.size, params)
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          errorMessage(err.response || err);
          reject(err.response);
        })
    })
  },

}
